<template>
	<view class="detail">
		<view class="detail-title">
			{{formData.title}}
		</view>
		<view class="detail-header">
			<view class="detail-header-img">
				<image :src="formData.author.avatar" mode="aspectFill"></image>
			</view>
			<view class="detail-header-content">
				<view class="detail-header-content-title">
					{{formData.author.author_name}}
				</view>
				<view class="detail-header-content-info">
					<text>{{formData.create_time}}</text>
					<text>{{formData.browse_count}} 浏览</text>
					<text>{{formData.thumbs_up_count}} 点赞</text>
				</view>
			</view>
			<button v-show="formData.is_author_like !==undefined" class="detail-header-btn" @click="follow(formData.author.id)">{{formData.is_author_like?'取消关注':'关注'}}</button>
		</view>
		<view class="detail-content">
			<view class="detail-html">
				<u-parse :content="formData.content" :noData="noData"></u-parse>
			</view>
			<view class="detail-comment">
				<view class="detail-comment-title">
					最新评论
				</view>
				<view class="detail-comment-content" v-for="item in comments" :key='item.comment_id'>
					<comment-box :item='item' @reply='reply'></comment-box>
				</view>
			</view>
		</view>
		<view class="detail-bottom">
			<view class="detail-bottom-text" @click="openComment">
				<text>谈谈你的看法</text>
				<uni-icons type="compose" size="16" color="#F07373"></uni-icons>
			</view>
			<view class="detail-bottom-icons">
				<view class="detail-bottom-icons-item" @click="open">
					<uni-icons type="chat" size="20" color="#F07373"></uni-icons>
				</view>
				<view class="detail-bottom-icons-item" @click="likeTap">
					<uni-icons :type="formData.is_like?'heart-filled':'heart'" size="20" color="#F07373"></uni-icons>
				</view>
				<view class="detail-bottom-icons-item" @click="thumbsUp">
					<uni-icons :type="formData.is_thumbs_up?'hand-thumbsup-filled':'hand-thumbsup'" size="20" color="#F07373"></uni-icons>
				</view>
			</view>
		</view>
		<uni-popup ref="popup" type="bottom" :maskClick="false">
			<view class="popup">
				<view class="popup-header">
					<text class="popup-header-item" @click="close">取消</text>
					<text class="popup-header-item" @click="submit">发布</text>
				</view>
				<view class="popup-content">
					<textarea v-model="commentVal" fixed placeholder="请输入你的评论" maxlength="200"/>
					<view class="popup-content-length">
						{{commentVal.length}}/200
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import uParse from '@/components/gaoyia-parse/parse.vue'
	export default {
		components:{
			uParse
		},
		data() {
			return {
				formData:{},
				noData:'<p style="text-align:center;color:#666">数据加载中...</p>',
				commentVal:'',
				comments:[],
				replyData:{}
			}
		},
		onLoad(query) {
			this.formData = JSON.parse(query.params)
			this.getArticelDetail(this.formData._id)
			this.getComments()
		},
		onReady() {
			// this.$refs.popup.open()
		},
		methods: {
			getArticelDetail(articleId){
				this.$api.getArticelDetail({articleId}).then(res=>{
					const {data} = res
					this.formData = data
				})
			},
			openComment(){
				this.$refs.popup.open()
			},
			close(){
				this.$refs.popup.close()
			},
			submit(){
				if(!this.commentVal){
					uni.showToast({
						title:'请输入评论',
						icon:'none'
					})
					return
				}
				this.updateComment({content:this.commentVal,...this.replyData});
			},
			updateComment(data){
				uni.showLoading()
				let params = {
					articleId:this.formData._id,
					...data
				}
				this.$api.updateComment(params).then(res=>{
					uni.hideLoading()
					uni.showToast({
						title:'评论成功'
					})
					this.getComments()
					this.commentVal=''
					this.replyData={}
					// 关闭弹出层
					this.close()
				}).catch(err=>{
					uni.hideLoading()
				})
			},
			getComments(){
				this.$api.getComment({acticleId:this.formData._id}).then(res=>{
					const {data} = res
					this.comments = data
				})
			},
			reply(e){
				this.replyData = {
					comment_id: e.comments.comment_id,
					is_reply: e.is_reply
				}
				if(e.comments.reply_id){
					this.replyData.reply_id=e.comments.reply_id
				}
				this.openComment()
			},
			follow(author_id){
				uni.showLoading()
				this.$api.updateAuthor({author_id}).then(res=>{
					uni.hideLoading()
					this.formData.is_author_like = !this.formData.is_author_like
					uni.$emit('update_author')
					uni.showToast({
						title:this.formData.is_author_like?'关注成功':'取消关注',
						icon:'none'
					})
				})
			},
			likeTap(articleId){
				uni.showLoading()
				this.$api.updateLike({articleId:this.formData._id}).then(res=>{
					uni.hideLoading()
					this.formData.is_like = !this.formData.is_like
					// 自定义事件
					uni.$emit('update_article','follow')
					uni.showToast({
						title:this.formData.is_like?'收藏成功':'取消收藏',
						icon:'none'
					})
				}).catch(err=>{
					uni.hideLoading()
				})
			},
			thumbsUp(){
				if(this.formData.is_thumbs_up){
					uni.showToast({
						title:'您已经点过赞了',
						icon:'none'
					})
					return
				}
				uni.showLoading()
				this.$api.updateThumbsUp({article_id:this.formData._id}).then(res=>{
					this.formData.is_thumbs_up = true
					this.formData.thumbs_up_count++
					uni.hideLoading()
					uni.showToast({
						title:'点赞成功',
						icon:'none'
					})
				}).catch(err=>{
					uni.hideLoading()
				})
			},
			open(){
				uni.navigateTo({
					url:'../detail-comments/detail-comments?id='+this.formData._id
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.detail {
		padding: 5px 0;
		padding-bottom: 54px;
		.detail-title {
			padding: 0 15px;
			font-size: 18px;
			font-weight: bold;
			color: #333333;
		}

		.detail-header {
			display: flex;
			align-items: center;
			margin-top: 10px;
			padding: 0 15px;

			.detail-header-img {
				flex-shrink: 0;
				width: 40px;
				height: 40px;
				border-radius: 50%;
				overflow: hidden;
				image {
					width: 100%;
					height: 100%;
				}
			}
			.detail-header-content {
				width: 100%;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				font-size: 12px;
				padding-left: 10px;
			
				.detail-header-content-title {
					font-size: 14px;
					color: #333333;
				}
			
				.detail-header-content-info {
					color: #999999;
			
					text {
						margin-right: 10px;
					}
				}
			}
			.detail-header-btn{
				flex-shrink: 0;
				height: 30px;
				font-size: 12px;
				background-color: $mk-base-color;
				color: #fff;
			}
		}
		
		.detail-content{
			min-height: 500px;
			margin-top: 15px;
			.detail-html{
				padding: 0 15px;
			}
			.detail-comment{
				margin-top: 30px;
				.detail-comment-title{
					padding: 10px 15px;
					font-size: 14px;
					color: #666;
					border-bottom: 1px solid #F5F5F5;
				}	
				.detail-comment-content{
					padding:0 15px;
					border-top: 1px #eee solid;
				}
			}
		}

		.detail-bottom{
			position: fixed;
			bottom: 0;
			left: 0;
			display: flex;
			align-items: center;
			width: 100%;
			height: 44px;
			border-top: 1px #f5f5f5 solid;
			box-sizing: border-box;
			background-color: #FFFFFF;
			.detail-bottom-text{
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-left: 10px;
				padding: 0 10px;
				width: 100%;
				height: 30px;
				border: 1px #ddd solid;
				border-radius: 5px;
				text{
					font-size: 14px;
					color: #999999;
				}
			}
			.detail-bottom-icons{
				display: flex;
				flex-shrink: 0;
				padding: 0 10px;
				.detail-bottom-icons-item{
					position: relative;
					display: flex;
					align-items: center;
					justify-content: center;
					width: 44px;
				}
			}
		}
	.popup{
		background-color: #FFFFFF;
		.popup-header{
			display: flex;
			justify-content: space-between;
			font-size: 16px;
			color: #666;
			border-bottom: 1px solid #F5F5F5;
			.popup-header-item{
				height: 50px;
				line-height: 50px;
				padding: 0 15px;
			}
		}
		.popup-content{
			width: 100%;
			padding: 15px;
			box-sizing: border-box;
			textarea{
				height: 150px;
				width: 100%;
			}
			.popup-content-length{
				display: flex;
				// 右对齐
				justify-content: flex-end;
				font-size: 12px;
				color: #999;
			}
		}
	}
	}
</style>
